﻿/**  版本信息模板在安装目录下，可自行修改。
* StaffLessonsLog.cs
*
* 功 能： N/A
* 类 名： StaffLessonsLog
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2015/4/11 14:29:40   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Catering_DBOpertion;//Please add references
namespace Catering_Operation
{
    /// <summary>
    /// 数据访问类:StaffLessonsLog
    /// </summary>
    public partial class StaffLessonsLog
    {
        public StaffLessonsLog()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("LessonID", "T_StaffLessonsLog");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int LessonID, int NodeID, int UserID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_StaffLessonsLog");
            strSql.Append(" where LessonID=@LessonID and NodeID=@NodeID and UserID=@UserID ");
            SqlParameter[] parameters = {
					new SqlParameter("@LessonID", SqlDbType.Int,4),
					new SqlParameter("@NodeID", SqlDbType.Int,4),
					new SqlParameter("@UserID", SqlDbType.Int,4)			};
            parameters[0].Value = LessonID;
            parameters[1].Value = NodeID;
            parameters[2].Value = UserID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Catering_Model.StaffLessonsLog model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_StaffLessonsLog(");
            strSql.Append("LessonID,NodeID,UserID,UserStartDate,UserEndDate,TestID,TestPoints,TestDate,AddUserID,AddTime,Addre)");
            strSql.Append(" values (");
            strSql.Append("@LessonID,@NodeID,@UserID,@UserStartDate,@UserEndDate,@TestID,@TestPoints,@TestDate,@AddUserID,@AddTime,@Addre)");
            SqlParameter[] parameters = {
					new SqlParameter("@LessonID", SqlDbType.Int,4),
					new SqlParameter("@NodeID", SqlDbType.Int,4),
					new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@UserStartDate", SqlDbType.DateTime),
					new SqlParameter("@UserEndDate", SqlDbType.DateTime),
					new SqlParameter("@TestID", SqlDbType.Int,4),
					new SqlParameter("@TestPoints", SqlDbType.Decimal,5),
					new SqlParameter("@TestDate", SqlDbType.DateTime),
					new SqlParameter("@AddUserID", SqlDbType.Int,4),
					new SqlParameter("@AddTime", SqlDbType.DateTime),
                    new SqlParameter("@Addre", SqlDbType.VarChar,500)};
            parameters[0].Value = model.LessonID;
            parameters[1].Value = model.NodeID;
            parameters[2].Value = model.UserID;
            parameters[3].Value = model.UserStartDate;
            parameters[4].Value = model.UserEndDate;
            parameters[5].Value = model.TestID;
            parameters[6].Value = model.TestPoints;
            parameters[7].Value = model.TestDate;
            parameters[8].Value = model.AddUserID;
            parameters[9].Value = model.AddTime;
            parameters[10].Value = model.Addre;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.StaffLessonsLog model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_StaffLessonsLog set ");
            strSql.Append("UserStartDate=@UserStartDate,");
            strSql.Append("UserEndDate=@UserEndDate,");
            strSql.Append("TestID=@TestID,");
            strSql.Append("TestPoints=@TestPoints,");
            strSql.Append("TestDate=@TestDate,");
            strSql.Append("AddUserID=@AddUserID,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("Addre=@Addre");
            strSql.Append(" where LessonID=@LessonID and NodeID=@NodeID and UserID=@UserID ");
            SqlParameter[] parameters = {
					new SqlParameter("@UserStartDate", SqlDbType.DateTime),
					new SqlParameter("@UserEndDate", SqlDbType.DateTime),
					new SqlParameter("@TestID", SqlDbType.Int,4),
					new SqlParameter("@TestPoints", SqlDbType.Decimal,5),
					new SqlParameter("@TestDate", SqlDbType.DateTime),
					new SqlParameter("@AddUserID", SqlDbType.Int,4),
					new SqlParameter("@AddTime", SqlDbType.DateTime),
					new SqlParameter("@LessonID", SqlDbType.Int,4),
					new SqlParameter("@NodeID", SqlDbType.Int,4),
					new SqlParameter("@UserID", SqlDbType.Int,4),
                    new SqlParameter("@Addre", SqlDbType.VarChar,500)};
            parameters[0].Value = model.UserStartDate;
            parameters[1].Value = model.UserEndDate;
            parameters[2].Value = model.TestID;
            parameters[3].Value = model.TestPoints;
            parameters[4].Value = model.TestDate;
            parameters[5].Value = model.AddUserID;
            parameters[6].Value = model.AddTime;
            parameters[7].Value = model.LessonID;
            parameters[8].Value = model.NodeID;
            parameters[9].Value = model.UserID;
            parameters[10].Value = model.Addre;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int LessonID, int NodeID, int UserID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_StaffLessonsLog ");
            strSql.Append(" where LessonID=@LessonID and NodeID=@NodeID and UserID=@UserID ");
            SqlParameter[] parameters = {
					new SqlParameter("@LessonID", SqlDbType.Int,4),
					new SqlParameter("@NodeID", SqlDbType.Int,4),
					new SqlParameter("@UserID", SqlDbType.Int,4)			};
            parameters[0].Value = LessonID;
            parameters[1].Value = NodeID;
            parameters[2].Value = UserID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.StaffLessonsLog GetModel(int LessonID, int NodeID, int UserID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 LessonID,NodeID,UserID,UserStartDate,UserEndDate,TestID,TestPoints,TestDate,AddUserID,AddTime,Addre from T_StaffLessonsLog ");
            strSql.Append(" where LessonID=@LessonID and NodeID=@NodeID and UserID=@UserID ");
            SqlParameter[] parameters = {
					new SqlParameter("@LessonID", SqlDbType.Int,4),
					new SqlParameter("@NodeID", SqlDbType.Int,4),
					new SqlParameter("@UserID", SqlDbType.Int,4)			};
            parameters[0].Value = LessonID;
            parameters[1].Value = NodeID;
            parameters[2].Value = UserID;

            Catering_Model.StaffLessonsLog model = new Catering_Model.StaffLessonsLog();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.StaffLessonsLog DataRowToModel(DataRow row)
        {
            Catering_Model.StaffLessonsLog model = new Catering_Model.StaffLessonsLog();
            if (row != null)
            {
                if (row["LessonID"] != null && row["LessonID"].ToString() != "")
                {
                    model.LessonID = int.Parse(row["LessonID"].ToString());
                }
                if (row["NodeID"] != null && row["NodeID"].ToString() != "")
                {
                    model.NodeID = int.Parse(row["NodeID"].ToString());
                }
                if (row["UserID"] != null && row["UserID"].ToString() != "")
                {
                    model.UserID = int.Parse(row["UserID"].ToString());
                }
                if (row["UserStartDate"] != null && row["UserStartDate"].ToString() != "")
                {
                    model.UserStartDate = DateTime.Parse(row["UserStartDate"].ToString());
                }
                if (row["UserEndDate"] != null && row["UserEndDate"].ToString() != "")
                {
                    model.UserEndDate = DateTime.Parse(row["UserEndDate"].ToString());
                }
                if (row["TestID"] != null && row["TestID"].ToString() != "")
                {
                    model.TestID = int.Parse(row["TestID"].ToString());
                }
                if (row["TestPoints"] != null && row["TestPoints"].ToString() != "")
                {
                    model.TestPoints = decimal.Parse(row["TestPoints"].ToString());
                }
                if (row["TestDate"] != null && row["TestDate"].ToString() != "")
                {
                    model.TestDate = DateTime.Parse(row["TestDate"].ToString());
                }
                if (row["AddUserID"] != null && row["AddUserID"].ToString() != "")
                {
                    model.AddUserID = int.Parse(row["AddUserID"].ToString());
                }
                if (row["AddTime"] != null && row["AddTime"].ToString() != "")
                {
                    model.AddTime = DateTime.Parse(row["AddTime"].ToString());
                }
                if (row["Addre"] != null && row["Addre"].ToString() != "")
                {
                    model.Addre = row["Addre"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LessonID,NodeID,UserID,UserStartDate,UserEndDate,TestID,TestPoints,TestDate,AddUserID,AddTime,Addre ");
            strSql.Append(" FROM T_StaffLessonsLog ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" LessonID,NodeID,UserID,UserStartDate,UserEndDate,TestID,TestPoints,TestDate,AddUserID,AddTime,Addre ");
            strSql.Append(" FROM T_StaffLessonsLog ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_StaffLessonsLog ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT *,ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by " + orderby);
            }
            else
            {
                strSql.Append("order by UserendDate desc");
            }
            strSql.Append(")AS Row from (");
            strSql.Append(" select L.*,S.ClassID,N.LessonName,N.Remarks from T_StaffLessonsLog AS L ");
            strSql.Append(" left join T_Staff AS S ON L.UserID=S.UserID ");
            strSql.Append(" left join T_StaffLessons AS N ON L.LessonID=N.LessonID ");
            strSql.Append(" where S.Status='O' ");

            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  BasicMethod
        #region  ExtensionMethod

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int ViewGetCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder("");
            strSql.Append(" select COUNT(*)  from T_StaffLessonsLog AS L ");
            strSql.Append(" left join T_Staff AS S ON L.UserID=S.UserID");
            strSql.Append(" left join T_StaffLessons AS N ON L.LessonID=N.LessonID");
            strSql.Append(" where S.Status='O' ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="strWhere"></param>
        /// <param name="orderby"></param>
        /// <returns></returns>
        public DataSet ViewGetList(int PageSize, int PageIndex, string strWhere, string orderby)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT *,ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by " + orderby);
            }
            else
            {
                strSql.Append("order by UserendDate desc");
            }
            strSql.Append(")AS Row from (");
            strSql.Append(" select L.*,S.ClassID,S.UserName,N.LessonName,N.Remarks,N.StartDate,N.EndDate,N.LearnTime,N.IsTest from T_StaffLessonsLog AS L ");
            strSql.Append(" left join T_Staff AS S ON L.UserID=S.UserID ");
            strSql.Append(" left join T_StaffLessons AS N ON L.LessonID=N.LessonID ");
            strSql.Append(" where S.Status='O' ");

            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(strWhere);
            }
            strSql.Append(" ) TT ) T");
            strSql.Append(" WHERE T.Row between " + (((PageIndex - 1) * PageSize) + 1) + " AND " + PageIndex * PageSize);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  ExtensionMethod
    }
}

